var comPageNum = 1;
function searchList(pageNum) {
    var params = {
        "customerId": $(".channelName").attr("data-id"),
        "invoiceStatusCode": $(".invoiceStatusCode").val(),//开票状态：0-待开具，1已开具
        "orderId": $(".orderId").val(),
        "applicationNo": $(".applicationNo").val(),
        "pageNum": pageNum,
        "pageSize": 20,
    }
    web.doAjax(root.channelInovice.applys, 'post',params, function (data) {
        if(data.errorCode == 0){
            $('.M-box3').pagination({
                pageCount:data.data.totalPage,
                current:pageNum,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    comPageNum = api.getCurrent();
                    searchList(api.getCurrent());

                }
            });
            renderList(data.data.result);
        }else {

        }
    },1);
}
function renderList(data) {
    var listHtml = "";
    data.forEach((item,index) => {
        if(item.invoiceStatusCode==2){
            var deliverBtn = "";
        }else {
            var deliverBtn = "<a class='ml-10' data-id='"+item.id+"' data-logisticsCompany='"+item.logisticsCompany+"' data-waybillId='"+item.waybillId+"' onclick='deliverFn(this)'>发货</a>";
        }
         // totalBatchTaxAmount 含税开票金额
        listHtml += "<tr class='text-c'>\n" +
            "                    <td>"+(index+1)+"</td>\n" +
            "                    <td title='"+item.channelName+"'>"+item.channelName+"</td>\n" +
            "                    <td title='"+item.applicationNo+"'>"+item.applicationNo+"</td>\n" +
            "                    <td title='"+item.invoiceTitle+"'>"+item.invoiceTitle+"</td>\n" +
            "                    <td title='"+item.invoiceTypeName+"'>"+item.invoiceTypeName+"</td>\n" +
            "                    <td title='"+item.totalBatchAmount+"'>"+item.totalBatchTaxAmount+"</td>\n" +
            "                    <td title='"+item.invoiceStatusCodeName+"'>"+item.invoiceStatusCodeName+"</td>\n" +
            "                    <td><a onclick='showDetailFn(\""+item.applicationNo+"\")'>查看</a>"+deliverBtn+"</td>\n" +
            "                </tr>";
    });
    $("#datatable tbody").html(listHtml);
}
/*重置表单*/
function resetFormFn() {
    $(".channelName").attr("data-id","");
}
/*查看详情*/
function showDetailFn(applicationNo){
    var index = layer.open({
        type: 2,
        title: "包含订单信息",
        content: "receiptDetail.html?applicationNo="+applicationNo,
        area: ['100%', '100%'],
    });
}
/*发货*/
function deliverFn(el){
    var inoviceId = $(el).attr("data-id");
    var logisticsCompany = $(el).attr("data-logisticsCompany");
    var waybillId = $(el).attr("data-waybillId");
    var index = layer.open({
        type: 2,
        title: "发货信息填写",
        content: "deliverPop.html?inoviceId="+inoviceId+"&logisticsCompany="+encodeURI(encodeURI(logisticsCompany))+"&waybillId="+waybillId,
        area: ['655px', '264px'],
        offset: ['100px', '100px']
    });
}
/*合作渠道*/
function searchChannelListFn(el) {
    $(el).attr("data-id","");
    var params = {
        "name":el.value,
    }
    web.doAjax(root.channelInovice.getChannelConfigByChannelName,'post',params, function (data) {
        if(data.errorCode == "0") {
            $(".channelList").css("display","block");
            if(data.data.length == 0) {
                var channelHtml = '<li class="clearfix pl-10 mb-5">无数据</li>';
                $(".channelList").html(channelHtml);
            } else {
                var channelHtml = '';
                data.data.forEach((item) => {
                    channelHtml += "<li class='clearfix pl-10 mb-5' onclick='selectChannelItem("+JSON.stringify(item)+")'>"+item.customerName+"</li>"
                })
                $(".channelList").html(channelHtml);
            }
        }
    },2);
}
$(document).bind("click", function(e) {
    if($(e.target).closest(".channelList").length == 0 && $(e.target).closest(".channelName").length == 0){
        $(".channelList").hide();
    }
});
function selectChannelItem(data){
    $(".channelName").val(data.customerName).attr({"data-id":data.customerId,"title":data.customerName});
    $(".channelList").hide();
}
invoiceStatus();
function invoiceStatus() {
    web.doAjax(root.channelInovice.invoiceStatus,'post', {}, function (data) {
        if(data.errorCode == "0") {
            var invoiceStatusHtml = '<option value="">全部</option>';
            data.data.forEach((item) => {
                invoiceStatusHtml += '<option value="'+item.invoiceApplyStatusCode+'">'+item.invoiceApplyStatusName+'</option>'
            })
            $(".invoiceStatusCode").html(invoiceStatusHtml);
        }
    },1);
}
/* 导出发票 */
function exportInvoice(){
    var pageNum = 1;
    var pageSize = 20000;
    var customerId = $(".channelName").attr("data-id");
    var invoiceStatusCode = $(".invoiceStatusCode").val();
    var orderId = $(".orderId").val();
    var applicationNo =  $(".applicationNo").val();
$('#export-invoice').attr('href',root.channelInovice.invoicesExport +
    '?pageNum='+ pageNum +'&pageSize='+pageSize+'&customerId='+customerId +'&invoiceStatusCode='+invoiceStatusCode +'&orderId='+orderId +'&applicationNo='+applicationNo)
}